package com.kaka.bus.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.bean.BankManagerBean;
import com.kaka.bus.listener.Listener;
import com.kaka.bus.util.U;

import java.util.List;

/**
 * Created by zhangqi on 2017/4/27.
 */

public class BankManagerAdapter extends RecyclerView.Adapter<BankManagerAdapter.BankManagerVH> {

    private List<BankManagerBean> mList;
    private Context mContext;

    public BankManagerAdapter(Context context, List<BankManagerBean> list){
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public BankManagerVH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BankManagerVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bankmanager,parent,false));
    }

    @Override
    public void onBindViewHolder(BankManagerVH holder, int position) {
        BankManagerBean bankManagerBean = mList.get(position);

        holder.mName.setText(bankManagerBean.getBankName());

        if("1".equals(bankManagerBean.getStatus())){
            holder.mRightTop.setImageResource(R.mipmap.ic_jiaobiao_zhuka);
            holder.mRightTop.setVisibility(View.VISIBLE);
        }else{
            holder.mRightTop.setVisibility(View.INVISIBLE);
        }

        if(bankManagerBean.getBankId() != null){
            int bg = -1;
            int bank_icon = -1;
            switch (bankManagerBean.getBankId()){
                case "1":
                    bg = R.mipmap.ic_bg_zhongguobank;
                    bank_icon = R.mipmap.icon_zhongguo;
                    break;
                case "2":
                    bg = R.mipmap.ic_bg_nongyebank;
                    bank_icon = R.mipmap.icon_nongye;
                    break;
                case "3":
                    bg = R.mipmap.ic_bg_gongshangbank;
                    bank_icon = R.mipmap.icon_gongshang;
                    break;
                case "4":
                    bg = R.mipmap.ic_bg_jianshebank;
                    bank_icon = R.mipmap.icon_jianhang;
                    break;
                case "5":
                    bg = R.mipmap.ic_bg_jiaotongbank;
                    bank_icon = R.mipmap.icon_jiaotong;
                    break;
                case "6":
                    bg = R.mipmap.ic_bg_pinganbank;
                    bank_icon = R.mipmap.icon_pingan;
                    break;
                case "7":
                    bg = R.mipmap.ic_bg_pufabank;
                    bank_icon = R.mipmap.icon_pufa;
                    break;
                case "8":
                    bg = R.mipmap.ic_bg_zhongxingbank;
                    bank_icon = R.mipmap.icon_zhongxin;
                    break;
                case "9":
                    bg = R.mipmap.ic_bg_xingyebank;
                    bank_icon = R.mipmap.icon_xingye;
                    break;
                case "10":
                    bg = R.mipmap.ic_bg_guangdabank;
                    bank_icon = R.mipmap.icon_guangda;
                    break;
                case "11":
                    bg = R.mipmap.ic_bg_youzhengchuxubank;
                    bank_icon = R.mipmap.icon_youzheng;
                    break;
                case "12":
                    bg = R.mipmap.ic_bg_shanghaibank;
                    bank_icon = R.mipmap.icon_shanghai;
                    break;
                default:
                    bg = R.mipmap.ic_bg_shanghaibank;
                    bank_icon = R.mipmap.icon_shanghai;
                    break;
            }

            if(bg != -1){
                holder.mIcon.setImageResource(bank_icon);
                holder.mBg.setBackgroundResource(bg);
            }

        }


        String cardNum = bankManagerBean.getBankNum();
        /**
         * 设置空格
         */

        holder.mCard.setText(U.connectString("**** **** **** ",cardNum.substring(cardNum.length()-4,cardNum.length())));

        holder.itemView.setOnClickListener(v -> {
            if(mListener != null){
                mListener.onClick(position,bankManagerBean);
            }
        });
    }

    private Listener.OnItemClickListener<BankManagerBean> mListener;

    public void setListener(Listener.OnItemClickListener<BankManagerBean> listener) {
        mListener = listener;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class BankManagerVH extends RecyclerView.ViewHolder{

        private RelativeLayout mBg;
        private ImageView mRightTop;
        private ImageView mIcon;
        private TextView mName;
        private TextView mCard;

        public BankManagerVH(View itemView) {
            super(itemView);
            mBg = (RelativeLayout) itemView.findViewById(R.id.item_bank_manager_bank);
            mRightTop = (ImageView) itemView.findViewById(R.id.item_bank_manager_righttop);
            mIcon = (ImageView) itemView.findViewById(R.id.item_bank_manager_bankicon);
            mName = (TextView) itemView.findViewById(R.id.item_bank_manager_bankname);
            mCard = (TextView) itemView.findViewById(R.id.item_bank_manager_banknum);
        }
    }
}
